package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ejb.*;
import entities.*;

/**
 * Servlet implementation class SeeClients
 */
@WebServlet("/SeeClients")
public class SeeFlights extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	CityRepositoryBeanLocal CRep;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SeeFlights() {
		super();
		// TODO Auto-generated constructor stub
	}

	private void procces(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			ArrayList<City> Cities = CRep.getAllCities();

			response.setContentType("text/html;charset=UTF-8");
			try (PrintWriter out = response.getWriter()) {

				out.println("<!DOCTYPE html>");
				out.println("<html>");
				out.println("<head>");
				out.println("<title>Servlet PostMessage</title>");
				out.println("</head>");
				out.println("<body>");
				for (int i = 0; i < Cities.size(); i++) {
					out.println("<div>");
					out.println(Cities.get(i).getCityCode());
					out.println("</div>");
					out.println("<div>");
					out.println(Cities.get(i).getCityName());
					out.println("</div>");	
					out.println("<div>");
					out.println(Cities.get(i).getId());
					out.println("</div>");
					out.println("<div>");
					out.println(Cities.get(i).getName());
					out.println("</div>");
					out.println("<div>");
					out.println(Cities.get(i).getCityLocation());
					out.println("</div>");
					out.println("<div>");
					out.println("---------------------------------");
					out.println("</div>");
				}
				out.println("</body>");
				out.println("</html>");

			}
		} catch (Exception e) {
			try {
				throw new ServletException(e.getMessage());
			} catch (ServletException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		procces( request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		procces( request,response);
	}

}
